﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DAL;
using BLL;

public partial class admin_Menus_List : System.Web.UI.Page
{
  private static int maxLevel;
  MenuBLL menuData = new MenuBLL();
  public static List<ListItem> listMode = new List<ListItem>();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      GetMode();     
      LoadPosition();      
      ShowData();
    }
  }
  public void GetMode()
  {
    if (listMode != null) listMode.Clear();
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xMode = xElement.Descendants("Menus").Descendants("Modes").Descendants("Mode");
    foreach (XElement item in xMode)
    {
      ListItem mode = new ListItem(item.Element("Name").Value, item.Attribute("Id").Value);
      listMode.Add(mode);
    }
  }
  private void LoadPosition()
  {    
    XElement xElement = XElement.Load(MyConfig.serverPath + "XML/Config.xml");
    var xPosition = xElement.Descendants("Menus").Descendants("Positions").Descendants("Position");
    foreach (XElement item in xPosition)
    {
      dropMenuPosition.Items.Add(new ListItem(item.Element("Name").Value, item.Attribute("Id").Value));      
    }
  }
  private void ShowData()
  {
    List<Menu_> listData = menuData.GetAll();
    int PageRow = Convert.ToInt32(dropPageSize.SelectedValue);
    gridView.PageSize = PageRow;
    if (dropMenuPosition.Items.Count > 0)
      listData = listData.Where(p => p.Position == Convert.ToInt32(dropMenuPosition.SelectedValue)).ToList();
    if (dropIsEnable.SelectedIndex != 0)
      listData = listData.Where(p => p.isEnable == Convert.ToBoolean(dropIsEnable.SelectedValue)).ToList();

    listData = listData.OrderBy(p => p.Sort).OrderBy(p => p.Parent).ToList();
    gridView.DataSource = listData;
    gridView.DataBind();

    //Bo mui ten len xuong dau va cuoi
    if (gridView.Rows.Count > 0)
    {
      gridView.Rows[0].Cells[7].Enabled = false;
      gridView.Rows[gridView.Rows.Count - 1].Cells[6].Enabled = false;
    }
  }
  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[2].Text);
            menuData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    try
    {
      if (e.Row.RowIndex >= 0) //Bo qua dong dau
      {
        int id = Convert.ToInt32(e.Row.Cells[2].Text);
        Menu_ menu = menuData.GetById(id);
        string strMode = e.Row.Cells[5].Text;
        ListItem mode = listMode.Where(p => p.Value == strMode).FirstOrDefault();
        e.Row.Cells[5].Text = mode.Text;

        if (e.Row.Cells[4].Text == "-1") e.Row.Cells[4].Text = "Menu gốc";
        else e.Row.Cells[4].Text = menuData.GetById(menu.Parent).Name;
        bool isEnable = menu.isEnable;

        //Icon check
        Image img = new Image();
        foreach (Control item in e.Row.Cells[9].Controls)
        {
          if (item.GetType() == typeof(Image))
          {
            img = (Image)item;
            break;
          }
        }
        if (isEnable)
          img.ImageUrl = "~/Images/check.png";
        else
        {
          img.ImageUrl = "~/Images/an.png";

        }
      }
    }
    catch { }
  }
  protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    lbThongBao.Text = "";
    try
    {
      gridView.PageIndex = e.NewPageIndex;
      ShowData();
    }
    catch { }
  } 
  protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
      menuData.DeleteByParent(id);
      menuData.Delete(id);
      Myfunction.ThongBao(lbThongBao, "Menu đã được xóa");

      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      if (e.CommandName.Equals("Up"))
      {
        int index = Convert.ToInt32(e.CommandArgument);
        int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
        int idSecond = Convert.ToInt32(gridView.Rows[index - 1].Cells[2].Text);

        if (menuData.GetById(idFirst).Position != menuData.GetById(idSecond).Position)
        {
          Myfunction.ThongBao(lbThongBao, "Chỉ được đổi thứ tự 2 menu có cùng vị trí");
          return;
        }

        menuData.SwapSort(idFirst, idSecond);
      }
      else if (e.CommandName.Equals("Down"))
      {
        int index = Convert.ToInt32(e.CommandArgument);
        int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[2].Text);
        int idSecond = Convert.ToInt32(gridView.Rows[index + 1].Cells[2].Text);

        if (menuData.GetById(idFirst).Position != menuData.GetById(idSecond).Position)
        {
          Myfunction.ThongBao(lbThongBao, "Chỉ được đổi thứ tự 2 menu có cùng vị trí");
          return;
        }

        menuData.SwapSort(idFirst, idSecond);
      }
      ShowData();

    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void btnAdd_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Menus/Details.aspx");
  }
  protected void btnVisible_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(false);
  }
  protected void btnEnable_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    lbThongBao.Text = "";
    Enable(true);
  }
  protected void btnXoaChon_Click(object sender, EventArgs e)
  {
    if ((int)Session["Role"] != 1)
    {
      lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
      return;
    }
    try
    {
      lbThongBao.Text = "";
      foreach (GridViewRow item in gridView.Rows)
      {
        foreach (Control ctrl in item.Cells[1].Controls)
        {
          if (ctrl.GetType() == typeof(CheckBox))
          {
            CheckBox check = (CheckBox)ctrl;
            if (check.Checked)
            {
              int id = Convert.ToInt32(item.Cells[2].Text);
              menuData.DeleteByParent(id);
              menuData.Delete(id);
            }
            break;
          }
        }
      }
      ShowData();
    }
    catch (Exception ex)
    {
      Myfunction.ThongBao(lbThongBao, ex.Message);
    }
  }
  protected void linkCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = true;
          break;
        }
      }
    }
  }
  protected void linkUnCheckAll_Click(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[1].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          check.Checked = false;
          break;
        }
      }
    }
  }
  protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();
  }
  protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
    ShowData();
  }
  protected void dropMenuPosition_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
    ShowData();

  }
}